package com.southminority.ethnic.service.course.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.southminority.ethnic.pojo.Course;
import com.southminority.ethnic.mapper.course.CourseMapper;
import com.southminority.ethnic.service.course.CourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.southminority.ethnic.controller.coures.vo.CourseDetailVo;
import com.southminority.ethnic.enums.CourseTypeEnum;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
* @author ASHTR
* @description 针对表【course(课程表)】的数据库操作Service实现
* @createDate 2025-01-20 10:00:00
*/
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {

    @Override
    public Page<CourseDetailVo> getCoursePageWithTeacher(Integer pageNum, Integer pageSize, Integer courseType) {
        // 创建分页对象
        Page<CourseDetailVo> page = new Page<>(pageNum, pageSize);
        
        // 执行关联查询
        Page<CourseDetailVo> resultPage = baseMapper.selectCoursePageWithTeacher(page, courseType);
        
        // 设置课程类型描述
        if (resultPage.getRecords() != null) {
            for (CourseDetailVo courseDetail : resultPage.getRecords()) {
                if (courseDetail.getCourseType() != null) {
                    courseDetail.setCourseTypeDesc(CourseTypeEnum.getDescriptionByCode(courseDetail.getCourseType()));
                }
            }
        }
        
        return resultPage;
    }

    @Override
    public Page<Course> getCoursePageWithKeyword(Page<Course> page, String keyword) {
        LambdaQueryWrapper<Course> wrapper = new LambdaQueryWrapper<>();
        
        // 如果有关键词，进行模糊查询
        if (StringUtils.hasText(keyword)) {
            wrapper.like(Course::getCourseName, keyword)
                    .or()
                    .like(Course::getCourseIntro, keyword);
        }
        
        // 按创建时间倒序排列
        wrapper.orderByDesc(Course::getCreateTime);
        
        return this.page(page, wrapper);
    }
} 